package com.xzg.completeMissionApply.controller;

import com.xzg.common.constants.Constant;
import com.xzg.common.exception.XZGException;
import com.xzg.common.page.PageQueryBean;
import com.xzg.common.resp.APIResult;
import com.xzg.common.vo.QueryCondition;
import com.xzg.completeMissionApply.entity.CompleteMissionApply;
import com.xzg.completeMissionApply.service.CompleteMissionService;
import com.xzg.user.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.directory.SearchResult;

@Controller
@RequestMapping("/complete")
public class CMissionApply {

    @Autowired
    private CompleteMissionService completeMissionService;

    /**
     *@Author Ye
     *Date 2018-03-26 18:00
     *@Description 完成任务工作流
     */
    @RequestMapping("/start")
    @ResponseBody
    public APIResult start(@RequestBody QueryCondition condition) throws XZGException {
         APIResult resp = new APIResult();
        try{
            completeMissionService.start(condition);
        }catch (XZGException e) {
            resp.setMessage(e.getMessage());
            resp.setCode(e.getStatusCode());
        }catch (Exception e) {
            resp.setMessage(e.getMessage());
            resp.setCode(Constant.RESP_STATUS_INTERNAL_ERROR);
        }
        return resp;
    }


    /**
     *@Author Ye
     *Date 2018-03-26 18:00
     *@Description 查看所有完成任务的内容
     */
    @RequiresPermissions("complete:list")
    @RequestMapping("/list")
    @ResponseBody
    public PageQueryBean list(QueryCondition condition) {
        PageQueryBean result = new PageQueryBean();
//        User user = new User();
//        user.setUsername("xiaoshu");
//        user.setId(2l);
          User user = (User) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
        completeMissionService.listTask(user.getUsername());
        return result;
    }

    @RequiresPermissions("complete:list")
    @RequestMapping("/approve")
    @ResponseBody
    public APIResult approve(@RequestBody QueryCondition condition) throws XZGException {
        int i = completeMissionService.approve(condition);
        APIResult resp = new APIResult();
        if(i == Constant.APPLY_STATUS_PSSS) {
            resp.setMessage("任务完成");
        }
        else if(i == Constant.APPLY_STATUS_REFUSE) {
            resp.setMessage("拒绝");
        } else {
            resp.setMessage("请求异常");
        }
        return resp;
    }


}
